$(function () {
    var employee_datagrid = $("#employee_datagrid");
    var emp_dialog = $("#emp_dialog");
    var emp_form = $("#emp_form");
    employee_datagrid.datagrid({
        url: '/applyer/query.do',
        fit: true,
        fitColumns: true,
        columns: [[
            {field: 'name', title: '应聘人姓名', width: 100},
            {field: 'job', title: '应聘职务', width: 100},
            {field: 'time', title: '应聘日期', width: 100},
            {field: 'hireForm', title: '聘用形式', width: 100},
            {field: 'workYear', title: '工作年数', width: 100},
            {field: 'education', title: '学历', width: 100},
            {field: 'relatedSkill', title: '相关技能', width: 100},
            {field: 'paymentRequest', title: '薪资要求', width: 100},
            {field: 'remark', title: '备注', width: 100},
        ]],
        singleSelect: true,
        pagination: true,
        toolbar: "#emp_btns"
    });
    emp_dialog.dialog({
        width: 300,
        height: 400,
        buttons: "#save_btns",
        closed: true,
        onClose: function () {
            emp_form.form('clear');
        }
    });

    $.extend($.fn.validatebox.defaults.rules, {
        maxdate: {
            validator: function(value, param){
                var d1 = $.fn.datebox.defaults.parser(param[0]);
                console.log(d1);
                var d2 = $.fn.datebox.defaults.parser(value);
                console.log(d2);
                return d2<=d1;
            },
            message: '不能超过此日期'
        }
    });

    $("a[data-cmd]").click(function () {
        var methodName = $(this).data('cmd');
        methodObject[methodName]();
    })
    var methodObject = {
        add: function () {
            emp_dialog.dialog('setTitle', '新增员工');
            emp_dialog.dialog('open');
        },
        del: function () {
            var row = employee_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选中一条数据", 'warning');
                return;
            }
            $.messager.confirm('确认对话框', '您确定要删除该信息吗？', function (r) {
                if (r) {
                    $.get("/applyer/delete.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示', '删除成功', 'info')
                            employee_datagrid.datagrid('reload');
                        } else {
                            $.messager.alert('温馨提示', data.msg, 'warning');
                        }
                    })
                }
            });
        },
        edit: function () {
            var row = employee_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选中一条数据", 'warning');
                return;
            }
            //回显角色数据
            emp_form.form('load', row
            );
            emp_dialog.dialog('setTitle', '编辑员工');
            emp_dialog.dialog('open');
        },
        reload: function () {
            employee_datagrid.datagrid('reload');
        },

        save: function () {
            emp_form.form('submit', {
                url: '/applyer/saveOrUpdate.do',
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('温馨提示', '保存成功', 'info')
                        methodObject.cancel();
                        employee_datagrid.datagrid('reload');
                    } else {
                        $.messager.alert('温馨提示', data.msg, 'error');
                    }
                }
            })
        },

        cancel: function () {
            emp_dialog.dialog('close');
        },
        query: function () {
            var keyword = $("[name=keyword]").val();
            var beginDate = $("[name=beginDate]").val();
            var endDate = $("[name=endDate]").val();
            employee_datagrid.datagrid('load', {
                keyword: keyword,
                beginDate: beginDate,
                endDate:endDate
            })
        }
    }
})


